package com.yupi.yuoj.test;

import com.yupi.yuoj.judge.codesandbox.CodeSandboxFactory;
import com.yupi.yuoj.judge.codesandbox.CodeSandboxProxy;
import com.yupi.yuoj.judge.codesandbox.CodeSandBox;
import com.yupi.yuoj.judge.codesandbox.impl.ExampleCodeSandbox;
import com.yupi.yuoj.judge.codesandbox.model.ExecuteCodeRequest;
import com.yupi.yuoj.judge.codesandbox.model.ExecuteCodeResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author 86134
 * @version 1.0
 * @description TODO
 * @date 2024/6/24 22:30
 */
@SpringBootTest
public class testSandbox {

    @Value("${codesandbox.type:example}")
    private String type;

    @Test
    void sandbox() {
        String code = "int main(){}";
        List<String> list = new ArrayList<>();
        ExecuteCodeRequest build = ExecuteCodeRequest.builder().language("java").
                code(code).inputList(list).build();
        CodeSandBox exampleCodeSandbox = new ExampleCodeSandbox();
        ExecuteCodeResponse executeCodeResponse = exampleCodeSandbox.executeCode(build);
        Assertions.assertNotNull(executeCodeResponse);
    }

    @Test
    void sandbox2() {
        String code = "public class Main {\n" +
                "    public static void main(String[] args) {\n" +
                "        int a = Integer.parseInt(args[0]);\n" +
                "        int b = Integer.parseInt(args[1]);\n" +
                "        System.out.println(\"结果: \"+(a+b) );\n" +
                "    }\n" +
                "}";
        List<String> list = new ArrayList<>();
        list.add("1 2");
        list.add("2 2");
        ExecuteCodeRequest build = ExecuteCodeRequest.builder().language("java").
                code(code).inputList(list).build();
        CodeSandBox exampleCodeSandbox = CodeSandboxFactory.newInstance(type);
        ExecuteCodeResponse executeCodeResponse = exampleCodeSandbox.executeCode(build);
        System.out.println(executeCodeResponse);
    }

    @Test
    void sandboxByProxy() {
        CodeSandboxProxy codeSandboxProxy = new CodeSandboxProxy(new ExampleCodeSandbox());
        ExecuteCodeResponse executeCodeResponse = codeSandboxProxy.executeCode(ExecuteCodeRequest.builder().code("int main(){}").language("java").build());
        System.out.println(executeCodeResponse);
    }


    public static void main(String[] args) {
        while (true){
            Scanner sc = new Scanner(System.in);
            String str = sc.next();
            CodeSandBox codeSandBox = CodeSandboxFactory.newInstance(str);

            String code = "int main(){}";
            List<String> list = new ArrayList<>();
            ExecuteCodeRequest build = ExecuteCodeRequest.builder().language("java").
                    code(code).inputList(list).build();
            ExecuteCodeResponse executeCodeResponse = codeSandBox.executeCode(build);
        }
    }
}
